import pandas as pd
from datetime import datetime


def error12_idf(df,dict_info_setting, columns):
    """
    校验身高和心率范围，标记异常值并添加批注

    :param df: 数据 DataFrame
    :param columns: 包含2个列名的列表 ['身高(cm)', '心率']
    :return: (styles_info, comments_info) 样式和批注信息
    """
    styles_info = {}
    comments_info = []

    height_xinlv = [int(info.strip()) for info in dict_info_setting["error12_idf"].split(':')[-1].split(",")]
    # 解包列名
    col_height, col_heart = columns

    for row_idx, row in df.iterrows():
        # ================= 身高校验 =================
        height = row[col_height]
        if pd.notna(height):
            if height > height_xinlv[0] or height < height_xinlv[1]:
                styles_info[(row_idx, col_height)] = 'error'  # 红色标记
                msg = f"身高({height}cm)超出正常范围({height_xinlv[1]}-{height_xinlv[0]}cm)"
                comments_info.append((row_idx, col_height, msg))

        # ================= 心率校验 =================
        heart = row[col_heart]
        if pd.notna(heart):
            if heart > height_xinlv[2] or heart < height_xinlv[3]:
                styles_info[(row_idx, col_heart)] = 'error'  # 红色标记
                msg = f"心率({heart}次/分)超出正常范围({height_xinlv[3]}-{height_xinlv[2]}次/分)"
                comments_info.append((row_idx, col_heart, msg))

    return styles_info, comments_info

